package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.util.IdWorker;
import com.macro.mall.mapper.SpsDataDiscussMapper;
import com.macro.mall.mapper.admin.BasicCollectionManMapper;
import com.macro.mall.model.BasicCollectionManParam;
import com.macro.mall.model.admin.BasicCollectionMan;
import com.macro.mall.pojo.CollectionManVo;
import com.macro.mall.query.BasicCollectionManQuery;
import com.macro.mall.service.SysCollectionManService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SysCollectionManServiceImpl implements SysCollectionManService {


    @Autowired
    private BasicCollectionManMapper collectionManMapper;


    @Autowired
    private SpsDataDiscussMapper dataDiscussMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public CommonResult edit(BasicCollectionManParam param) {
        if (param.getId() == null || param.getId().equals("") || param.getId().length() <= 0) {
            BasicCollectionMan collectionMan = new BasicCollectionMan();
            BeanUtils.copyProperties(param, collectionMan);
            collectionMan.setId(String.valueOf(idWorker.nextId()));
            collectionMan.setCreateTime(new Date());
            collectionMan.setCatgId(Long.valueOf(param.getCatgId()));
            int row = collectionManMapper.insertSelective(collectionMan);
            return result(row, "添加成功");
        }
        BasicCollectionMan collectionMan = new BasicCollectionMan();
        collectionMan.setCatgId(Long.valueOf(param.getCatgId()));
        BeanUtils.copyProperties(param, collectionMan);
        int row = collectionManMapper.updateByPrimaryKeySelective(collectionMan);
        return result(row, "修改成功");
    }

    @Override
    public CommonResult delete(String id) {
        int row = collectionManMapper.deleteByPrimaryKey(id);
        return result(row, "删除失败");
    }

    @Override
    public CommonResult getList(BasicCollectionManParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(param.getPage(), param.getPageSize());
        BasicCollectionManQuery query = new BasicCollectionManQuery();
        BeanUtils.copyProperties(param, query);
        List<CollectionManVo> collectionManList = collectionManMapper.selectList(query);
        PageInfo<CollectionManVo> pageInfo = new PageInfo<>(collectionManList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return CommonResult.success(resultMap);
    }

    @Override
    public CommonResult getOne(String id) {
        CollectionManVo basicCollectionMan = collectionManMapper.selectByPrimaryKey(id);
        return CommonResult.success(basicCollectionMan);
    }

    @Override
    public CommonResult init() {
        List<CollectionManVo> collectionManVos = collectionManMapper.selectList(null);
        for (CollectionManVo collectionManVo : collectionManVos) {
            if (collectionManVo.getDomainName() != null && collectionManVo.getDomainName().length() > 1) {
                collectionManVo.setDomainName(subDomainName(collectionManVo.getDomainName()));
            }
        }
        for (CollectionManVo collectionManVo : collectionManVos) {
            collectionManMapper.updateByPrimaryKeySelective(collectionManVo);
        }
        return CommonResult.success(null);
    }

    private String subDomainName(String name){
        String[] split = name.split("/");
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < split.length; i++ ) {
            if(i == 0) {
                sb.append(split[i]).append("//");
            }else if(i == 2) {
                sb.append(split[i]);
            }
        }
        return sb.toString();
    }


    /***
     * 判断修改数据库返回值 返回
     * @param row
     * @param msg
     * @return
     */
    private CommonResult result(int row, String msg) {
        if (row > 0) {
            return CommonResult.success(null);
        }
        return CommonResult.failed(msg);
    }
}
